import { reqAddGoods, reqChangeOneChecked, reqCheckedAll, reqCheckedGood, reqDeleteOne, reqShopCartList } from "@/api"

export default {
    state: {
        shopCartList:[] 
    },
    mutations: {
        UPDATE_SHOPCARTLIST(state, shopCartList) {
            state.shopCartList = shopCartList
        }
    },
    actions: {
        // 添加商品购物车(修改数量)
        async getAddGoods({ commit }, { skuId, skuNum }) {
            const res = await reqAddGoods(skuId, skuNum)
            if (res.code === 200) {
                return 'ok'
            } else {
                return Promise.reject('fail')
            }
        },
        // 查询购物车商品列表
        async getShopCartList({commit}) {
            const res = await reqShopCartList()
            if (res.code === 200) {
                commit('UPDATE_SHOPCARTLIST', res.data[0]?.cartInfoList)
            }
        },
        // 修改单个选中状态
        async getChangeOneChecked({commit},{skuId,isChecked}) {
            const res = await reqChangeOneChecked(skuId, isChecked)
            if (res.code === 200) {
                return 'ok'
            } else {
                return Promise.reject('fail')
            }
        },
        // 全选和全不选
        async getCheckedAll({ commit }, { isChecked, skuIdList }) {
            const res = await reqCheckedAll(isChecked, skuIdList)
            console.log(isChecked, skuIdList);
            if (res.code === 200) {
                return 'ok'
            } else {
                return Promise.reject('fail')
            }
        },
        // 删除单个商品
        async getDeleteOne({ commit }, skuId) {
            const res = await reqDeleteOne(skuId)
            if (res.code === 200) {
                return 'ok'
            } else {
                return Promise.reject()
            }
        },
        // 删除选中商品
        async getCheckedGood({ commit }, skuIdList) {
            const res = await reqCheckedGood(skuIdList)
            if (res.code === 200) {
                return 'ok'
            } else {
                return Promise.reject()
            }
        }
    },
    getters:{}
}